

#include "singleThreadFunc.h"


void * singleThreadFunc(void * arg){
    PthreadPoolS * pps = (PthreadPoolS *)arg;
    printf("I'm ready!(long command)\n"); 
    while(1){
        pthread_mutex_lock(&pps->lock1);
        while(pps->rdq.size == 0){
            pthread_cond_wait(&pps->cond,&pps->lock1);
        }
        int netfd = deleteQueueNode(&pps->rdq); 
        MYSQL * mysql = mysql_init(NULL);
        MYSQL * ret = mysql_real_connect(mysql,"localhost","root","123","netdisk",0,NULL,0);
        pthread_mutex_unlock(&pps->lock1);
        int epollfd = epoll_create(1);
        addEpollNode(epollfd,netfd);
        struct epoll_event readyset[1024];
        int readynum1 = epoll_wait(epollfd,readyset,1024,10000);
        if(readynum1 == 0){
            printf("等待超时,线程退出再监听\n");
            mysql_close(mysql);
            continue;
        }

        Train train;
        char JWT[1024] = {0};
        recv(readyset[0].data.fd,&train.length,sizeof(train.length),MSG_WAITALL);
        recv(readyset[0].data.fd,train.data,train.length,MSG_WAITALL);
        memcpy(JWT,train.data,train.length);
        
        if(decode(JWT) == -1){
            printf("非puts、gets客户!\n");
            send(netfd,"0",1,MSG_NOSIGNAL);
            printf("线程退出再监听\n");
            continue;
        }
        send(netfd,"1",1,MSG_NOSIGNAL);

        epoll_wait(epollfd,readyset,1024,-1);


        char * command;
        recvT(readyset[0].data.fd,&command);
        printf("command:%s\n",command);

        char * username;
        recvT(readyset[0].data.fd,&username);
        printf("%s\n",username);

        char prev_path[1024] = {0};        // 父文件的文件路径（对应表项就是path)
        recv(readyset[0].data.fd,&train.length,sizeof(train.length),MSG_WAITALL);
        recv(readyset[0].data.fd,train.data,train.length,MSG_WAITALL);
        memcpy(prev_path,train.data,train.length);
        printf("prev_path:%s\n",prev_path);


        char real_path[] = "../../../netdisk";
        if(strcmp(command,"puts") == 0){
            printf("intoputsDispose\n");
            putsDispose(readyset[0].data.fd,username,mysql,prev_path,real_path);
            printf("outputsDispose\n");
        }
        if(strcmp(command,"gets") == 0){
            printf("intogetsDispose\n");
            getsDispose(readyset[0].data.fd,username,mysql,prev_path,real_path);
            printf("outgetsDispose\n");
        }
        
        free(username);
        free(command);

    }

    pthread_exit(NULL);
}
